package com.zhiyou100.taxi.acction;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.zhiyou100.taxi.entity.CarIn;
import com.zhiyou100.taxi.service.CarInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-26 10:38
 * @Description
 */
@Controller
@RequestMapping("/carin")
public class CarInController {
    @Autowired
    CarInService carInService;

    @RequestMapping("/show.action")
    public String show() {
//        return "busines/caInList";
        return "forward:/carin/page.action";
    }

    @RequestMapping("/page.action")
    public String page(Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<CarIn> carInPageInfo = carInService.pageCar(pageNum);
        request.getSession().setAttribute("carInPageInfo", carInPageInfo);
        return "busines/caInList";
    }

    @RequestMapping("/save.action")
    public String save(CarIn carIn, HttpServletRequest request) {
        String key = carInService.getPrimaryKey();
        carIn.setCarInId(key);
        System.out.println("保存数据====》carIn:" + carIn);
        int num = carInService.insert(carIn);
        if (num > 0) {
            request.setAttribute("msg", "保存数据成功");
        } else {
            request.setAttribute("mmsg", "保存数据失败");
        }
        return "forward:/carin/page.action";
    }

    @RequestMapping("/deleteBatch.action")
    public String deleteBatch(String carInIds, HttpServletRequest request) {
        String[] split = carInIds.split(",");
        HashSet<String> set = new HashSet<>();
        set.addAll(Arrays.asList(split));
        int num = carInService.deleteByPrimaries(set);
        if (num > 0) {
            request.setAttribute("msg", "保存数据成功");
        } else {
            request.setAttribute("mmsg", "保存数据失败");
        }
        return "forward:/carin/page.action";
    }

    @RequestMapping("/getOne.action")
    public String getOne(String carInId, HttpServletRequest request) {
        CarIn carIn = carInService.getOneByPrimaryKey(carInId);
        request.setAttribute("carIn", carIn);
        return "busines/car-in-edit";
    }

    @RequestMapping("/update.action")
    public String update(CarIn carIn, HttpServletRequest request) {
        int num = carInService.updateByPrimaryKey(carIn);
        if (num > 0) {
            request.setAttribute("msg", "保存数据成功");
        } else {
            request.setAttribute("mmsg", "保存数据失败");
        }

        return "forward:/carin/page.action";
    }

    @RequestMapping("/getOneUnion.action")
    public String getOneUnion(String carInId, HttpServletRequest request) {
        CarIn carIn = carInService.getOneUnionByPrimaryKey(carInId);
        System.out.println(carIn);
        System.out.println(carIn.getCarCheck());
        System.out.println(carIn.getCarOut());
        System.out.println(carIn.getCar());
        System.out.println(carIn.getUser());
        request.setAttribute("carIn", carIn);
        return "busines/car-in-show";
    }
}
